package com.hut.oa.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.hut.oa.model.bean.Menu;
import com.hut.oa.model.bean.Status;
import com.hut.oa.model.bean.Type;
import com.hut.oa.model.dao.system.MenuDao;
import com.hut.oa.model.dao.system.StatusDao;
import com.hut.oa.model.dao.system.TypeDao;
import com.hut.oa.service.SystemService;

@Controller
@RequestMapping("/system")
public class SystemController {
	
	@Autowired
	SystemService ss;
	
	@Autowired
	MenuDao md;
	
	@Autowired
	TypeDao td;
	
	@Autowired
	StatusDao sd;

	/**
	 * 菜单列表
	 * @return
	 */
	@RequestMapping("/menu")
	public String toMenu(Model model,HttpSession session){
		//model.addAttribute("menus", md.findByTypeOrderBySeq("menu"));
		model.addAttribute("menus", ss.getDatas());
		return "system/menu";
	}
	
	@RequestMapping("/menu/datas")
	@ResponseBody
	public List<Menu> getMenus(){
		return ss.getDatas();
	}
	
	@RequestMapping(value="/menu/save/source",method=RequestMethod.POST)
	@ResponseBody
	public void saveSource(String text,String url,Integer power,Long parentId){
		md.save(new Menu(text, url, "icon-setup", md.findById(parentId), "operate", power));
	}
	
	/**
	 * 到菜单添加表单页面
	 * @return
	 */
	@RequestMapping("/menu/add")
	public String toAddMenu(@RequestParam(required=false,name="id")Long id,Model model){
		model.addAttribute("fatherMenus", md.findByParentIsNullOrderBySeq());
		if(id!=null){
			model.addAttribute("father", md.findById(id));
		}
		return "system/addMenu";
	}
	
	/**
	 * 获取单条菜单数据
	 * @return
	 */
	@RequestMapping("/menu/getone")
	public String oneMenu(@RequestParam("id")Long id,Model model){
		model.addAttribute("fatherMenus", md.findByParentIsNullOrderBySeq());
		model.addAttribute("menu", md.findById(id));
		return "system/addMenu";
	}
	
	/**
	 * 保存菜单表单
	 * @return
	 */
	@RequestMapping("/menu/save")
	@ResponseBody
	public void menuSave(Menu menu,HttpSession session){
		md.save(menu);
	}
	
	/**
	 * 删除单条菜单数据
	 * @return
	 */
	@RequestMapping("/menu/delete")
	@ResponseBody
	public void deleteMenu(@RequestParam("id")Long id,HttpSession session){
		md.delete(id);
	}
	
	/**
	 * 更改菜单排序
	 * @param thisId
	 * @param otherId
	 */
	@RequestMapping("/menu/updateseq")
	@ResponseBody
	public void updateSeq(@RequestParam("thisId")Long thisId,@RequestParam("otherId")Long otherId,HttpSession session){
		ss.updateSeq(thisId, otherId);
	}
	
	@RequestMapping("/menu/open")
	@ResponseBody
	public void updateOpen(@RequestParam("id")Long id,@RequestParam("open")boolean open,HttpSession session){
		ss.updateOpen(id, open);
	}
	
	/**
	 * 状态列表
	 * @return
	 */
	@RequestMapping("/state")
	public String toState(Model model,@PageableDefault(size=10,sort="module")Pageable pageable){
		model.addAttribute("states", sd.findAll(pageable));
		return "system/state";
	}
	
	/**
	 * 添加状态页面
	 * @return
	 */
	@RequestMapping("/addstate")
	public String toAddState(){
		return "system/addState";
	}
	
	/**
	 * 获得单条数据到添加页面
	 * @return
	 */
	@RequestMapping("/state/getone")
	public String oneState(@RequestParam("id")Long id,Model model){
		model.addAttribute("status", sd.findById(id));
		return "system/addState";
	}
	
	/**
	 * 保存状态表单
	 * @return
	 */
	@RequestMapping("/state/save")
	@ResponseBody
	public void stateSave(Status status,HttpSession session){
		sd.save(status);
		session.setAttribute("status", ss.getStatus());
	}
	
	/**
	 * 删除单个状态
	 * @param id
	 */
	@RequestMapping("/state/delete")
	@ResponseBody
	public void deleteState(@RequestParam("id")Long id){
		sd.delete(id);
	}
	
	/**
	 * 类型列表
	 * @return
	 */
	@RequestMapping("/type")
	public String toStyle(Model model,@PageableDefault(size=10,sort="module")Pageable pageable){
		model.addAttribute("types", td.findAll(pageable));
		return "system/type";
	}
	
	/**
	 * 添加类型页面
	 * @return
	 */
	@RequestMapping("/addtype")
	public String toAddType(){
		return "system/addType";
	}
	
	/**
	 * 获得单条数据到添加页面
	 * @return
	 */
	@RequestMapping("/type/getone")
	public String oneType(@RequestParam("id")Long id,Model model){
		model.addAttribute("type", td.findById(id));
		return "system/addType";
	}
	
	/**
	 * 保存类型表单
	 * @return
	 */
	@RequestMapping("/type/save")
	@ResponseBody
	public void typeSave(Type type,HttpSession session){
		td.save(type);
		session.setAttribute("types", ss.getTypes());
	}
	
	/**
	 * 删除单个状态
	 * @param id
	 */
	@RequestMapping("/type/delete")
	@ResponseBody
	public void deleteType(@RequestParam("id")Long id){
		td.delete(id);
	}
}
